package cn.rongdao.jrkabao.util.growingio;

import android.annotation.SuppressLint;

import com.growingio.android.sdk.collection.GrowingIO;
import com.mifengkong.frtools.app.FRApplication;
import com.mifengkong.frtools.system.systemserver.FRSystemService;
import com.mifengkong.frtools.util.FRSharedPreferences;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.Map;

import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.constant.SPConstant;
import cn.rongdao.jrkabao.dao.UserDao;
import cn.rongdao.jrkabao.util.talkingdata.TalkingDataChannelIdTransformName;

/**
 * Created by jiangyongxing on 2017/6/1.
 * 描述：为埋点而生的工具类
 */

public class GIOForBuringPointUtil {
    /**
     * 保存product_name
     *
     * @param product_name
     */
    public static void saveProductName(String product_name) {
        FRSharedPreferences.setStringDefault(SPConstant.PRODUCT_NAME, product_name);
    }

    public static String getProductName() {
        return FRSharedPreferences.getStringDefault(SPConstant.PRODUCT_NAME, "");
    }

    /**
     * 带参数
     *
     * @param eventName
     * @param map
     */
    public static void track(String eventName, Map<String, String> map) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("system", "Android");
            jsonObject.put("source_id", TalkingDataChannelIdTransformName.uMengChannelIdTransformName(FRApplication.getInstance()));
            jsonObject.put("package_name", BuildConfig.APPLICATION_ID);
            jsonObject.put("app_name", FRApplication.getInstance().getResources().getString(R.string.app_name));
            jsonObject.put("dev_version", BuildConfig.VERSION_NAME);
            jsonObject.put("user_id", UserDao.getInstance().getUserId());
            Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                jsonObject.put(entry.getKey(), entry.getValue());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GrowingIO.getInstance().track(eventName, jsonObject);
    }

    /**
     * 不带参数
     *
     * @param eventName
     */
    @SuppressLint("HardwareIds")
    public static void track(String eventName) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("system", "Android");
            jsonObject.put("source_id", TalkingDataChannelIdTransformName.uMengChannelIdTransformName(FRApplication.getInstance()));
            jsonObject.put("package_name", BuildConfig.APPLICATION_ID);
            jsonObject.put("app_name", FRApplication.getInstance().getResources().getString(R.string.app_name));
            jsonObject.put("dev_version", BuildConfig.VERSION_NAME);
            jsonObject.put("user_id", UserDao.getInstance().getUserId());
            jsonObject.put("utrace", FRSystemService.getTelephonyManager().getDeviceId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GrowingIO.getInstance().track(eventName, jsonObject);
    }

    /**
     * 带参数,只有product_name
     *
     * @param eventName
     */
    public static void track(String eventName, String product_name) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("system", "Android");
            jsonObject.put("channel", TalkingDataChannelIdTransformName.uMengChannelIdTransformName(FRApplication.getInstance()));
            jsonObject.put("package_name", BuildConfig.APPLICATION_ID);
            jsonObject.put("appedition", BuildConfig.VERSION_NAME);
            jsonObject.put("product_name", product_name);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        GrowingIO.getInstance().track(eventName, jsonObject);
    }

    /**
     * 事件名称：埋点_实名二要素验证成功
     * 触发条件：当用户实名二要素验证成功时
     */
    public static void realNameValidateSuccess() {
        track("name&idno_1");
    }
}